package org.express.portal;

/**
 * Used for holds an interceptor chain.
 * 
 */
class InterceptorChainImpl implements InterceptorChain
{
	private final Interceptor[] interceptors;
	private int index = 0;
	private Object result = null;

	InterceptorChainImpl(Interceptor[] interceptors)
	{
		this.interceptors = interceptors;
	}

	Object getResult()
	{
		return result;
	}

	public void doInterceptor(Execution execution) throws Exception
	{
		if (index == interceptors.length)
			result = execution.execute();
		else
		{
			// must update index first, otherwise will cause stack overflow:
			index++;
			interceptors[index - 1].intercept(execution, this);
		}
	}
}
